package com.xiaoma.myaudience.biz.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.biz.model.HomeInfoModel;
import com.xiaoma.myaudience.biz.task.CategoryOptionInfoTask;
import com.xiaoma.myaudience.util.activity.BaseTabActivity;

/**
 * 查看评论页面.
 * 
 */
public class ReviewActivity extends BaseTabActivity implements OnClickListener{
	private Context mContext;

	private static final int MENU_REFRESH = Menu.FIRST;
	
	public static final String TAG = "CategoryActivity";
	// 按相册
	public static final String PHOTO_SPEC = "score";
	// 按评论
	public static final String REVIEW_SPEC = "time";
	
	public static final String OPTION_TAG = "_option";
	public static final String FILM_OPTION = "film_option";
	public static final String TV_OPTION = "tv_option";
	public static final String DOC_OPTION = "doc_option";
	public static final String FROM_ACTIVITY = "from";
	
	private static final int HEAD_TAB = 1;
	private static final int MID_TAB = HEAD_TAB + 1;
    public static String mTitle;

	private TabHost mTabHost;
	private CategoryOptionInfoTask mOptionInfoTask;
	private View mCurrentContentView;
    private int mCurrentTab = -1;
    public static String mEventId;
    private String mPicId;
    private String mType;
    private String mFromActivity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.review_activity);
		
		 Bundle bundle = getIntent().getExtras();
	        if (bundle != null) {
	        	mEventId = bundle.getString("eventid");
	        	mPicId = bundle.getString(HomeInfoModel.PARAM_PICID);
	        	mType = bundle.getString(HomeInfoModel.PARAM_TYPE);
	        	mFromActivity = bundle.getString(FROM_ACTIVITY);
			}

		mContext = this;
		findViewById(R.id.btn_back).setOnClickListener(this);
		Button comment = (Button) findViewById(R.id.show_comments_back_original);
		if ("LoginActivity".equals(mFromActivity)) {
			comment.setVisibility(View.GONE);
		} else {
			comment.setVisibility(View.VISIBLE);
		}
		comment.setText("评论");
		comment.setOnClickListener(this);

		setupTabs();
	}

	
	/**
	 * 返回context
	 * @return
	 */
	public Context getContext() {
		return mContext;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		int t = 0;
		if("1".equals(mFromActivity)){
			t = 1;
		}
		mTabHost.setCurrentTab(t);
	}
	
	/**
	 * TabHost 初始化
	 */
	private void setupTabs() {
		mTabHost = getTabHost();
		mTabHost.setOnTabChangedListener(this);
		mTabHost.setup();

		addTab(PHOTO_SPEC, R.string.revice_tab_photo, HEAD_TAB);
		addTab(REVIEW_SPEC, R.string.revice_tab_review, MID_TAB);
		int t = 0;
		if("1".equals(mFromActivity)){
			t = 1;
		}
		mTabHost.setCurrentTab(t);
	}
	
	/**
	 * 添加tab
	 * @param spec 
	 * @param titleid
	 * @param type
	 */
	private void addTab(String spec, int titleid, int type) {
		Intent intent = new Intent();
		intent.putExtra("tab_index", type);
		intent.putExtra("eventid", mEventId);
		intent.putExtra(HomeInfoModel.PARAM_TYPE, mType);
		intent.putExtra(HomeInfoModel.PARAM_PICID, mPicId);
		intent.setClass(this, ReviewListActivity.class);
		intent.putExtra(TAG, spec);
		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(spec);
		localTabSpec.setIndicator(getTabIndicator(titleid, type));
		localTabSpec.setContent(intent);
		mTabHost.addTab(localTabSpec);
	}

    /**
     * 生成tab indicator
     * @param resourceid 图片资源id
     * @param type tab位置处于开始，中间还是末尾，不同的位置放置不同的图片
     * @return
     */
	private View getTabIndicator(int resourceid, int type) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.category_tabwidget_layout, null);
        TextView button = (TextView) view.findViewById(R.id.category_tab);     
        button.setText(resourceid);
		switch(type) {
		case HEAD_TAB:
			button.setBackgroundResource(R.drawable.category_head_tab_indicator);
			break;
		case MID_TAB:
			button.setBackgroundResource(R.drawable.category_head_tab_indicator);
			break;
		}
		
		return view;
	}


	@Override
    public void onTabChanged(String tabId) {
        TabHost tabHost = getTabHost();

        Animation enterAnimation = null;
        Animation exitAnimation = null;

        final int last = mCurrentTab;
        final int now = tabHost.getCurrentTab();

        if (last != -1) {
            if (last > now) {
                enterAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_enter_from_left);
                exitAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_exit_from_left);
            } else if (last < now) {
                enterAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_enter_from_right);
                exitAnimation = AnimationUtils.loadAnimation(this, R.anim.tab_exit_from_right);
            }
        }

        if (mCurrentContentView != null && exitAnimation != null) {
            mCurrentContentView.setAnimation(exitAnimation);
        }

        mCurrentContentView = tabHost.getCurrentView();
        mCurrentTab = now;

        if (enterAnimation != null) {
            mCurrentContentView.setAnimation(enterAnimation);
        }
        
    }

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			ReviewActivity.this.finish();
			break;
		case R.id.show_comments_back_original:
			if (!TextUtils.isEmpty(mTitle)) {
				Intent intent = new Intent(ReviewActivity.this, CaptureActivity.class);
				intent.putExtra("eventid", mEventId);
				intent.putExtra("title", mTitle);
				ReviewActivity.this.startActivity(intent);
				ReviewActivity.this.finish();
			}
			break;

		default:
			break;
		}
	}

}
